package com.lanxin.jet.tools;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class JetResponse {

    public final static int RES_UNKNOW			= 0;	//未知状态
	public final static int RES_OK				= 1;	//操作成功
	public final static int RES_FAIL			= 2;	//操作失败
	public final static int RES_NOCHAN			= 102;	//操作结果无变化
	public final static int RES_PARAMF			= 103;	//操作失败：参数类型错误，或者缺失
	public final static int RES_TOKENF			= 104;	//操作失败：身份验证失败，或者权限不足
	public final static int RES_REFUSE			= 105;	//操作失败：因安全策略，系统拒绝操作
	public final static int RES_NOTHAS			= 106;	//操作失败：要操作的数据或者目标不存在

	@SuppressWarnings({"serial"})
	public final static Map<Integer, String> RES_MAP = new HashMap<Integer, String>(){{
		put(RES_UNKNOW, "未知状态");
		put(RES_OK, "操作成功");
		put(RES_FAIL, "操作失败");
		put(RES_NOCHAN, "操作结果无变化");
		put(RES_PARAMF, "操作失败：参数类型错误，或者缺失");
		put(RES_TOKENF, "操作失败：身份验证失败，或者权限不足");
		put(RES_REFUSE, "操作失败：因安全策略，系统拒绝操作");
		put(RES_NOTHAS, "操作失败：要操作的数据或者目标不存在");
	}};

    /** 响应码 */
    private int code;

    /** 结果集 */
    private Object result;

    /** 错误集 */
    private Object error;

    /**
     * 响应结果
     */
    public void success() {
        this.code = RES_OK;
        this.result = RES_MAP.get(RES_OK);
    }

    /**
     * 响应结果
     * @param result
     */
    public void success(Object result) {
        this.code = RES_OK;
        this.result = result;
    }

    /**
	 * 响应错误码
	 * @param code
	 */
	public void error(int code) {
        this.code = code;
		String error = RES_MAP.get(code);
		if (!error.isEmpty()) {
            this.error = error;
		} else {
			this.error = "操作失败";
		}
    }
    
    /**
     * 响应错误码
     * @param code
     * @param error
     */
    public void error(int code, Object error) {
        this.code = code;
        this.error = error;
    }

    /**
     * 输出Map结果
     * @return
     */
    public Map<String, Object> map() {
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("code", code);
        data.put("result", result);
        data.put("error", error);
        data.put("datetime", JetDate.dateNow(null));
        return data;
    }
    
}